#include "windowdao.h"
#include"../utils/database.h"
#include"approveexam.h"
#include"QSqlQuery"
#include<QSqlRecord>
#include<QVariant>
#include<QSqlError>
#include<QDebug>


/*
 * edit by raoxiaoyang
 * test by raoxiaoyang
 */

WindowDao::WindowDao()
{
    QSqlQuery result=QSqlQuery(*DataBase::getInstance());
}

bool WindowDao::insertWindow(WindowInfo &win)
{
    QString sql="insert into WindowInfo(id,name,address,organization_id,status) value(%1,'%2','%3',%4,'%5')";
    sql=sql.arg(win.id)
            .arg(QString::fromStdString(win.name))
            .arg(QString::fromStdString(win.address))
            .arg(win.organization_id)
            .arg(win.status);
    if(result.exec(sql))
        return true;
    else{
        qDebug()<<result.lastError().text();
    }
    return false;
}

bool WindowDao::alterWindow(WindowInfo win)
{
    QString sql="update WindowInfo set name='%1',address='%2',organization_id=%3,status='%4' where id=%5";
    sql=sql.arg(QString::fromStdString(win.name))
           .arg(QString::fromStdString(win.address))
           .arg(win.organization_id)
           .arg(win.status)
           .arg(win.id);
    if(result.exec(sql))
        return true;
    else{
        qDebug()<<result.lastError().text();
    }
    return false;
}

bool WindowDao::delWindow(WindowInfo win)
{
    QString sql="delete from WindowInfo where id=%1";
    sql=sql.arg(win.id);
    if(result.exec(sql))
        return true;
    else{
        qDebug()<<result.lastError().text();
    }
    return false;
}

bool WindowDao::selectWindow(WindowInfo &win)
{
    QString sql="select * from WindowInfo where id=%1";
    sql=sql.arg(win.id);

    if(!result.exec(sql))
        return false;

    result.next();

    win.name = result.value("name").toString().toStdString();
    win.address = result.value("address").toString().toStdString();
    win.organization_id = result.value("organization_id").toInt();
    win.status = result.value("status").toInt();

    return true;

}

vector<WindowInfo> WindowDao::selectUserWindow(User user)
{
    vector<WindowInfo> ans;
    QString sql="select * from WindowInfo where id in (select window_id from windowApproval where user_id='%1')";
    sql=sql.arg(QString::fromStdString(user.phone));

    if(!result.exec(sql))
        return ans;

    while(result.next()){
        WindowInfo win;
        win.id = result.value("id").toInt();
        win.name = result.value("name").toString().toStdString();
        win.address = result.value("address").toString().toStdString();
        win.organization_id = result.value("organization_id").toInt();
        win.status = result.value("status").toInt();
        ans.push_back(win);
    }
    return ans;
}

vector<WindowInfo> WindowDao::selectUserApplyWindow(User user)
{
    vector<WindowInfo> ans;
    QString sql="select * from WindowInfo where id in (select window_id from windowApproval where user_id='%1' and status='%2')";
    sql=sql.arg(QString::fromStdString(user.phone))
           .arg(WindowInfo::STATUS_failture);

    if(!result.exec(sql))
        return ans;

    while(result.next()){
        WindowInfo win;
        win.id = result.value("id").toInt();
        win.name = result.value("name").toString().toStdString();
        win.address = result.value("address").toString().toStdString();
        win.organization_id = result.value("organization_id").toInt();
        win.status = result.value("status").toInt();
        ans.push_back(win);
    }
    return ans;

}

vector<WindowInfo> WindowDao::selectOrgWindow(OrganizationInfo org)
{
    vector<WindowInfo> ans;
    QString sql="select * from WindowInfo where organization_id=%1";
    sql=sql.arg(org.id);

    if(!result.exec(sql))
        return ans;

    while(result.next()){
        WindowInfo win;
        win.id = result.value("id").toInt();
        win.name = result.value("name").toString().toStdString();
        win.address = result.value("address").toString().toStdString();
        win.organization_id = result.value("organization_id").toInt();
        win.status = result.value("status").toInt();
        ans.push_back(win);
    }
    return ans;
}

/*
 * 查询空闲窗口（user用户可以申请加入的窗口
 * */
vector<WindowInfo> WindowDao::selectSpareWindow(User user)
{
    vector<WindowInfo> ans;
    QString sql="select id from WindowInfo  where status = '%1' and "
                "organization_id in "
                "(select organization_id from joinApproval "
                "where status = '%2' and user_id = '%3');";
    sql=sql.arg(WindowInfo::STATUS_spare)
            .arg(ApproveExam::STATU_PASS)
            .arg(QString::fromStdString(user.phone));
    if(!result.exec(sql))
    {
        qDebug()<<result.lastError().text();
    }
    while (result.next()) {
        WindowInfo win;
        win.id=result.value("id").toInt();
        //插入结果集
        ans.push_back(win);
    }
    for(unsigned int i=0;i<ans.size();i++)
        selectWindow(ans[i]);

    return ans;
}
